﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTV_V2.BussinessLogic;
using QLTV_V2.BussinessObject;
using QLTV_V2.DataAccess;

namespace QLTV_V2
{
    public partial class FrmCTKiemke : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public FrmCTKiemke()
        {
            InitializeComponent();
        }
        Utilities uti = new Utilities();
        SachBUS sachBUS = new SachBUS();
        CTKiemKe ct = new CTKiemKe();
        public int dk;//Bien kiem tra tao moi hay co san
        public string maphieukk;
       
        private void FrmCTKiemke_Load(object sender, EventArgs e)
        {
            txt_MaphieuKK.Text = maphieukk;
            if (dk == 2)
            {
                dgv_CTKiemKe.DataSource = sachBUS.getCTKiemKe(maphieukk);
                for (int i = 0; i < dgv_CTKiemKe.Rows.Count; i++)
                {
                    dgv_CTKiemKe.Rows[i].Cells[0].Value = i + 1;
                }
            }
        }

        private void btn_XoamucKK_Click(object sender, EventArgs e)
        {
            try
            {
                int dong = dgv_CTKiemKe.CurrentCell.RowIndex;
                string mactkk = dgv_CTKiemKe.Rows[dong].Cells["MaCTKK"].Value.ToString();

                if (uti.deleteTable("CTKiemKe", "MaCTKK", mactkk) == true)
                {
                    dgv_CTKiemKe.DataSource = sachBUS.getCTKiemKe(maphieukk);
                    for (int i = 0; i < dgv_CTKiemKe.Rows.Count; i++)
                    {
                        dgv_CTKiemKe.Rows[i].Cells[0].Value = i + 1;
                    }
                }
            }
            catch
            { 
                
            }
        }

        private void btn_Kiemke_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_MaBansach.Text != "")
                {
                    if (uti.CheckExitsValue("BanSach", "MaBanSach", txt_MaBansach.Text) == false)//ton tai
                    {
                        if (Check_Exit_DataGrid() == true)
                        {
                            ct = getTT_CTKiemKe();
                            if (sachBUS.insertCTPhieuKK(ct) == true)
                            {
                                dgv_CTKiemKe.DataSource = sachBUS.getCTKiemKe(maphieukk);
                                for (int i = 0; i < dgv_CTKiemKe.Rows.Count; i++)
                                {
                                    dgv_CTKiemKe.Rows[i].Cells[0].Value = i + 1;
                                }
                            }
                        }
                        else
                            MessageBox.Show("Mã sách đã bị trùng!", "Thông báo");
                    }
                    else
                    {
                        MessageBox.Show("Mã sách không tồn tại!", "Thông báo");
                    }
                }
                else
                    MessageBox.Show("Chưa nhập mã sách!", "Thông báo");
            }
            catch
            { 
            
            }
        }
       
        CTKiemKe getTT_CTKiemKe()
        {
            string lastID = uti.GetLastID_DK("CTKiemKe", "MaPhieuKK", maphieukk, "MaCTKK", "MaCTKK", maphieukk + "-0000");
            string nextID = uti.GetNextID(lastID, maphieukk + "-");
            ct.MaCTKK = nextID.ToUpper();
            ct.MaPhieuKK=txt_MaphieuKK.Text.ToUpper();
            ct.MaBanSach = txt_MaBansach.Text.ToUpper();
            return ct;
        }
       
        bool Check_Exit_DataGrid()
        {
            int n = dgv_CTKiemKe.Rows.Count;
            string[] luu = new string[n];
            for (int i = 0; i < n - 1; i++) //Luu vao mang
                luu[i] = dgv_CTKiemKe.Rows[i].Cells["MaBanSach"].Value.ToString();
            for (int i = 0; i < n; i++) //Lay ra kiem tra
                if (txt_MaBansach.Text == luu[i])
                    return false;
            return true;
        }
       
        private void btn_Inphieu_Click(object sender, EventArgs e)
        {
            frmReportPhieuKK frm = new frmReportPhieuKK();
            frm.maphieuKK = txt_MaphieuKK.Text;
            frm.dk = 1;
            frm.ShowDialog();
        }

        private void dgv_CTKiemKe_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            uti.dgv_GetSTT_RowHeader(sender, e, dgv_CTKiemKe);
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
